package io.renren.modules.basedata.service.impl;


import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.valid.VehiclePlateCheckValidateRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.valid.VehiclePlateCheckValidateResponse;
import io.renren.common.utils.CompletionStr;
import io.renren.modules.basedata.service.VehiclePlateCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class VehiclePlateCheckServiceImpl implements VehiclePlateCheckService {

    private static final Logger log = LoggerFactory.getLogger(VehiclePlateCheckServiceImpl.class);

    @Override
    public String checkVehiclePlate(String vehiclePlate, String vehicleColor, String vehicleType) throws Exception {
        log.info("开始校验车牌");
        String result="";
        if(vehiclePlate!=null&&vehicleColor!=null&&vehicleType!=null&&
                !"".equalsIgnoreCase(vehiclePlate)&&!"".equalsIgnoreCase(vehicleColor)&&!"".equalsIgnoreCase(vehicleType)){
            // (1)找到对应的协议模型（VehiclePlateCheckValidateRequest）
            VehiclePlateCheckValidateRequest request=new VehiclePlateCheckValidateRequest();
            //(2)按照接口文档要求，填写数据
            request.setVehiclePlate(vehiclePlate);
            request.setVehicleColor(Integer.parseInt(vehicleColor));
            request.setVehicleType(Integer.parseInt(vehicleType));
            log.info("车牌="+vehiclePlate+"车牌颜色="+Integer.parseInt(vehicleColor)+"车型="+Integer.parseInt(vehicleType));
            //(3)获取发送文件名
            String fileName= ETCCommHelper.getFileName(request,"65");
            System.out.println("向部中心请求"+fileName);
            try {
                //(4)指定响应的协议模型（IssuerUploadResponse），调用upload 发送数据
                VehiclePlateCheckValidateResponse response=ETCCommHelper.sdtsUpload(fileName, request, VehiclePlateCheckValidateResponse.class);
                //(5)发送成功处理（这里为示例，简单的将响应模型转为json字符串输出，各省根据实际情况处理）
                log.info("车牌"+vehiclePlate+"唯一性返回结果："+response.toJson());
                if(response!=null){
                    result=String.valueOf(response.getResult())+String.valueOf(response.getReason())+ CompletionStr.compStr(response.getInfo().replaceAll(" ", ""), 100);
                    log.info("返回result结果："+result);
                    log.info("返回result结果长度："+result.getBytes("GBK").length);
                }else{
                    result = "25"+CompletionStr.compStr("与部中心连接网络异常，不允许做业务", 100);
                    log.info("返回result结果："+result);
                }
            }catch (IOException e) {
                //TODO 网络异常处理,各省补全
                log.info("IO异常:"+e.getMessage());
                result = "25"+CompletionStr.compStr("系统异常，不允许做业务", 100);
                e.printStackTrace();
            } catch (ApiRequestException apie) {
                //TODO 系统自定义异常处理，各省补全
                log.info("Apie异常:"+apie.getMessage());
                result = "25"+CompletionStr.compStr("与部中心连接网络异常，不允许做业务", 100);//25 2:失败 5:网络异常
                apie.printStackTrace();
            }

        }

        return result;
    }
}
